package com.dingding.web.activity.action;

import java.util.List;
import javax.annotation.Resource;
import com.dingding.common.core.beans.Activity;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.enums.AcitvityPageRedirectEnum;
import com.dingding.facde.activity.enums.RulePageRedirectEnum;
import com.dingding.facde.activity.service.IActivityShowService;
import com.dingding.facde.activity.service.IWechatActivityService;
import com.dingding.web.base.action.BaseAction;

/**
 * @ClassName: ActivityAction
 * @Description: 活动模块action
 * @author yuHaiQing
 * @date 2016年3月24日 下午8:22:56
 */
@SuppressWarnings("all")
public class ActivityAction extends BaseAction {

	// 声明活动模块业务逻辑的接口
	@Resource(name = "activityShowService")
	private IActivityShowService activityShowService;

	// 微信活动接口
	@Resource(name = "wechatActivityService")
	private IWechatActivityService wechatActivityService;
	
	private String bizId;	
	private String userId;	

	/**
	 * @Title: getActivityList
	 * @Description: 获取活动列表
	 * @author yuHaiQing
	 * @return String 返回类型
	 */
	public String getActivityList() {

		// 获取全部的活动列表
		List<Activity> activities = activityShowService.getActivityList();

		// 把返回信息封装为json
		String jsonp = super.getResJsonp(activities);
		super.write(jsonp);

		return null;
	}

	/**
	 * @Description:获取活动详情
	 * @author fengshuonan
	 * @return String
	 */
	public String getActivityDetail() {

		String snId = getPara("snId");

		Activity activity = this.activityShowService.getActivityInfo(snId);

		super.write(super.getResJsonp(activity));

		return NONE;
	}

	/**
	 * @Description: 页面跳转代理，根据传来的类型，和主键id跳转到相应页面
	 * @author fengshuonan
	 * @return String
	 * 
	 * 修改人：车焕波
	 * 修改时间：216-07-18
	 * 修改内容：添加新的计价规则跳转
	 * 修改版本：V2.3.1
	 * 
	 */
	public String pageRedirectProxy() {

		String pageRedirectType = getPara("bizType"); // 需要跳转到页面的业务类型

		String bizId = getPara("bizId"); // 需要跳转到页面的具体业务id（比如规则id，活动id）

		return this.getResultPage(pageRedirectType, bizId);
	}

	/**
	 * @Description: 统计各个渠道微信关注数
	 * @author fengshuonan
	 */
	public String statWechatAttention() {
		String shopId = super.getPara("shopId");

		if (super.validateRefresh() == false) {
			this.wechatActivityService.addShopStatNum(Integer.valueOf(shopId));
		}

		return SUCCESS;
	}

	/**
	 * @Description:根据请求的业务类型和业务id决定页面要跳转到哪里
	 * @param pageRedirectType 业务类型
	 * @param bizId 业务id
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	private String getResultPage(String pageRedirectType, String bizId) {
		
		if(ToolUtil.isBlank(pageRedirectType)){
			return NONE;
		}

		switch (pageRedirectType) {
				
			case "activity":
				
				if(ToolUtil.isBlank(bizId)){
					return NONE;
				}
				for(AcitvityPageRedirectEnum value : AcitvityPageRedirectEnum.values()){
					if (bizId.equals(value.getCode())) {
						return value.getResult();
					}
				}
			
			case "rule":
				
				if(ToolUtil.isBlank(bizId)){
					return NONE;
				}
				if( bizId.equals(RulePageRedirectEnum.JFGZ_RULE.getCode()) || bizId.equals(RulePageRedirectEnum.PCFY_RULE.getCode()) ){
					return RulePageRedirectEnum.JFGZ_RULE.getResult();
				}
				
			default:
				return pageRedirectType;
		}
	}

	public String getBizId() {
		return bizId;
	}
	public void setBizId(String bizId) {
		this.bizId = bizId;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
}
